<?php
/**
 * Created by PhpStorm.
 * User: liyang
 * Date: 2018/1/23  Time: 下午1:40
 * Description: 适当性风险变动
 */


class RisklogController extends BaseController
{

    //产品风险等级（0：其他，1：低风险R1，2：中低风险R2，3：中风险R3，4：中高风险R4，5：高风险R5）
    const pro_level = [
        0 => '其他',
        1 => '低风险R1',
        2 => '中低风险R2',
        3 => '中风险R3',
        4 => '中高风险R4',
        5 => '高风险R5'
    ];
    //用户风险等级（0：其他，1：保守型，2：稳健性，3：平衡型，4：成长型，5：进取型）
    const user_level = [
        0 => '其他',
        1 => '保守型C1',
        2 => '稳健性C2',
        3 => '平衡型C3',
        4 => '成长型C4',
        5 => '进取型C5'
    ];

    /**
     * 风险变动列表
     */
    public function listAction()
    {
        $id = $this->header['uid'];
        if (empty($id)) {
            $this->callback('', MISS_REQUEST_PARAMS, '你还没有登录');
        }
        $list = RiskLogModel::select('id', 'title', 'create_time', 'pro_level', 'user_level')->where([
            'uid' => $id,
            'status' => '0'
        ])->get();
        if ($list->isEmpty()) {
            $this->callback(['list' => []], '0000', '没有适当性数据');
        } else {
            $list = $list->toArray();
        }
        $h5BaseUrl = getCommonConfig('h5_baseurl');

        foreach ($list as $k => $v) {
            $data['list'][$k]['id'] = $v['id'];
            $data['list'][$k]['header'] = '重要风险提示';
            $data['list'][$k]['url'] = urlencode($h5BaseUrl . "/risklog/notice?id={$v['id']}");
            $data['list'][$k]['title'] = $v['title'];
            $data['list'][$k]['create_time'] = $v['create_time'];
            $data['list'][$k]['pro_level'] = self::pro_level[$v['pro_level']];
            $data['user_level'] = self::user_level[$v['user_level']];
        }
        $this->callback($data, '0000', '查询成功');
    }

    /**
     * 风险变动风险变动H5请求展示数据
     */
    public function getRiskAction()
    {
        $id = $this->getRequest()->getParam('id');

        if (empty($id)) {
            $this->callback([], MISS_REQUEST_PARAMS, '缺少参数,参数为空');
        }
        $data = RiskLogModel::query()->where(['id' => $id])->first();
        if(!empty($data)) {
            $data = $data->toArray();
        }
        $this->callback($data);

    }

    /**
     * 风险变动H5请求处理数据
     */
    public function resetRiskAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) {
            $this->callback([], MISS_REQUEST_PARAMS, '缺少参数,参数为空');
        }

        $riskLog = RiskLogModel::query()->where(['id' => $id, 'status' => 0])->first();
        if (empty($riskLog)) {
            $this->callback([], MISS_REQUEST_PARAMS, '数据不存在或状态不匹配');
        }
        $riskLog->status = 2;
        $riskLog->update_time = date('Y-m-d H:i:s');
        $riskLog->from_terminal = 1;
        if ($riskLog->save()) {
            $this->callback([], '0000', '风险变动处理成功');
        }
        $this->callback([], '', '风险变动处理失败');
    }

    /**
     * 风险变动数据条数
     */
    public function getRiskNumAction()
    {
        $id = $this->header['uid'];
        if (empty($id)) {
            $this->callback('', MISS_REQUEST_PARAMS, '你还没有登录');
        }

        $riskNum = RiskLogModel::where('status', '0')->where('uid', $id)->count();
        $data['num'] = $riskNum;
        $this->callback($data);

    }

}